package test.n00100;


import java.util.Arrays;

public class Problem00141 {
    public static void main(String[] args) {
        println(new String[]{"flower", "flow", "flight"});
        println(new String[]{"dog", "racecar", "car"});
        println(new String[]{"reflower", "flow", "flight"});
    }

    public static void println(String[] strs) {
        Solution ss = new Solution();
        System.out.println(Arrays.asList(strs) + "," + ss.longestCommonPrefix(strs));
    }

    public static class Solution {

        /**
         * 编写一个函数来查找字符串数组中的最长公共前缀。
         * <p>
         * 如果不存在公共前缀，返回空字符串 ""。
         * <p>
         * 提示：
         * <p>
         * 1 <= strs.length <= 200
         * 0 <= strs[i].length <= 200
         * strs[i] 仅由小写英文字母组成
         * <p>
         * 解题思路:
         * 纵向扫描
         *
         * @param strs
         * @return
         */
        public String longestCommonPrefix(String[] strs) {
            String str1 = strs[0];
            for (int i = 0; i < str1.length(); i++) {
                char c = str1.charAt(i);
                for (int j = 1; j < strs.length; j++) {
                    if (strs[j].length() == i || c != strs[j].charAt(i)) {
                        return str1.substring(0, i);
                    }
                }
            }
            return str1;
        }
    }
}
